var cheerio = require("cheerio");
var async = require("async");
var utils = require('../dao/utils');
var Spider = require("./spider");

var insertDB = require("./insert.db");

var spider = new Spider('http://www.manhuagui.com/list/index_p*PageReplace*.html', {
    "Cookie": "_ga = GA1.2.1913663911.1522314747; country=CN; __gads=ID = fb4f7b06325b36a4: T=1522315337: S=ALNI_MbG1B36GPx0wRNzjV5ShuUQs6p1OA; _gid=GA1.2.1364757544.1524193529; SL_GWPT_Show_Hide_tmp=1; SL_wptGlobTipTmp=1; Hm_lvt_38a1bab61660f620209480de377747ed=1522314748, 1522314975, 1522315333, 1524193530; BB_plg=pm; MarketGidStorage=% 7B% 220 % 22 % 3A% 7B% 22svspr% 22 % 3A% 22http% 3A% 2F% 2Fwww.manhuagui.com % 2Fcomic% 2F27800% 2F% 22 % 2C% 22svsds% 22 % 3A1% 2C% 22TejndEEDj% 22 % 3A% 22LRRLj6KkB% 22 % 7D% 2C% 22C217817% 22 % 3A% 7B% 22page% 22 % 3A1% 2C% 22time% 22 % 3A1524194319222% 7D% 7D; _gat=1; Hm_lpvt_38a1bab61660f620209480de377747ed=1524200837",
    "Host": "www.manhuagui.com",
    "Referer": "http://www.manhuagui.com/list/",
    "Upgrade-Insecure-Requests": 1,
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
});

var host = "http://www.manhuagui.com";
spider.start(fun_urls, fun_pages, fun_detail);



function fun_urls(document) {
    var $ = cheerio.load(document);

    var liArry = $('#contList li'), urls = [];

    for (var i = 0; i < liArry.length; i++) {
        var item = liArry.eq(i);
        if (item.hasClass('ad_360_item')) {
            continue;
        }
        var url = host + item.find('.ell').find("a").attr('href');

        urls.push(url);
    }

    return urls;
}


function fun_pages(document) {
    var $ = cheerio.load(document);

    var href = $('#AspNetPager1 a').eq(-1).attr('href'), totalPage;

    totalPage = href ? href.match(/\d+/)[0] : null;
    return totalPage ? totalPage : 603;//266
}

function fun_detail(url, document) {

    var $ = cheerio.load(document);


    var chaptersLis = $(".chapter-list li");
    var chapter = [];
    for (var i = 0; i < chaptersLis.length; i++) {
        var _cur = chaptersLis.eq(i);

        chapter.push({
            href: utils.host(url) + _cur.find("a").eq(0).attr('href'),
            chapter: _cur.find("a").eq(0).attr('title') + "(" + utils.trim(_cur.find("i").eq(0).text()) + ")"
        });
    }



    var _title = $(".book-title").text();
    _title = utils.trim(_title);

    // info
    var info = $("#intro-all").text();



    var time = $(".status .red").eq(1).text();
    time = utils.trim(time);
    time = utils.timeFormat(time);

    var data = {
        "title": _title || name,
        "href": url,
        "img": $('.book-cover img').attr('src'),
        "auth": $(".detail-list li").eq(1).find("a").eq(-1) ? utils.trim($(".detail-list li").eq(1).find("a").eq(-1).text()) : null,

        "auth_href": utils.host(url) + $(".detail-list li").eq(1).find("a").eq(-1).attr('href'),

        "news": chapter.length > 0 ? chapter[0]['chapter'] : null,
        "news_href": chapter.length > 0 ? chapter[0]['href'] : null,

        "isFinish": utils.trim($(".status .red").eq(0).text()) == '连载中' ? false : true,

        "updateTime": time,

        "type": utils.trim($(".detail-list li").eq(1).find("span").eq(0).text().replace("漫画剧情：", '')),

        "letter": utils.letter(_title),

        "info": utils.trim(info),
        "domain": utils.host(url),
        "isUpdate": true,
        "chapters": chapter
    };

    insertDB(data);



}